home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / Primitve.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  19.2 KB  |  584 lines

  1. /*
  2. //   (C) COPYRIGHT International Business Machines Corp. 1993
  3. //   All Rights Reserved
  4. //   Licensed Materials - Property of IBM
  5. //   US Government Users Restricted Rights - Use, duplication or
  6. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. //
  8.  
  9. //
  10. // Permission to use, copy, modify, and distribute this software and its
  11. // documentation for any purpose and without fee is hereby granted, provided
  12. // that the above copyright notice appear in all copies and that both that
  13. // copyright notice and this permission notice appear in supporting
  14. // documentation, and that the name of I.B.M. not be used in advertising
  15. // or publicity pertaining to distribution of the software without specific,
  16. // written prior permission. I.B.M. makes no representations about the
  17. // suitability of this software for any purpose.  It is provided "as is"
  18. // without express or implied warranty.
  19. //
  20. // I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
  22. // BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  24. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  25. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. // Authors:  Barry Minor, John Spitzer, IBM AWS Graphics Systems (Austin)
  28. //
  29. */
  30.  
  31. #include <math.h>
  32. #include "Primitve.h"
  33. #include "Image.h"
  34. #include <malloc.h>
  35.  
  36. void new_Primitive(PrimitivePtr this)
  37. {
  38.     new_Drawn((DrawnPtr)this);
  39.     this->traversalData = 0;
  40.     /* Set virtual functions */
  41.     this->SetState = Primitive__SetState;
  42.     this->delete = delete_Primitive;
  43. }
  44.  
  45. void delete_Primitive(TestPtr thisTest)
  46. {
  47.     PrimitivePtr this = (PrimitivePtr)thisTest;
  48.  
  49.     delete_Drawn(thisTest);
  50. }
  51.  
  52. void Primitive__SetProjection(PrimitivePtr this, int dimension)
  53. {
  54.     /* set projection matrix */
  55.     glMatrixMode(GL_PROJECTION);
  56.     glLoadIdentity();
  57.     if (this->projection == Parallel) {
  58.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1.5);
  59.     } else { /* Perspective */
  60.     gluPerspective(90.0, 1.0, 0.5, 2.5);
  61.     }
  62.     glMatrixMode(GL_TEXTURE);
  63.     glLoadIdentity();
  64.     glMatrixMode(GL_MODELVIEW);
  65.     glLoadIdentity();
  66.  
  67. }
  68.  
  69. int Primitive__SetState(TestPtr thisTest)
  70. {
  71.     PrimitivePtr this = (PrimitivePtr)thisTest;
  72.     int i;
  73.     int size;
  74.  
  75.     /* set parent state */
  76.     if (Drawn__SetState(thisTest) == -1) return -1;
  77.  
  78.     /* set own state */
  79.  
  80.     if (!this->fogMode) {
  81.         glDisable(GL_FOG);
  82.     } else {
  83.         glFogi(GL_FOG_MODE, this->fogMode);
  84.         glFogf(GL_FOG_START, 0.0);
  85.         glFogf(GL_FOG_END, 1.0);
  86.         glEnable(GL_FOG);
  87.     }
  88.  
  89.     if (!this->alphaTest) {
  90.         glDisable(GL_ALPHA_TEST);
  91.     } else {
  92.         glAlphaFunc(this->alphaTest, this->alphaRef);
  93.         glEnable(GL_ALPHA_TEST);
  94.     }
  95.  
  96.     if (!this->stencilTest) {
  97.         glDisable(GL_STENCIL_TEST);
  98.     } else {
  99.         glStencilFunc(this->stencilTest, 0, 0xffffffff);
  100.         glEnable(GL_STENCIL_TEST);
  101.     }
  102.  
  103.     if (!this->depthTest) {
  104.         glDisable(GL_DEPTH_TEST);
  105.     } else {
  106.         glDepthFunc(this->depthTest);
  107.         glEnable(GL_DEPTH_TEST);
  108.     }
  109.  
  110.     glDepthMask(this->depthMask);
  111.  
  112.     if (!this->blend) {
  113.         glDisable(GL_BLEND);
  114.     } else {
  115. #ifdef GL_EXT_blend_color
  116.     if (strstr(this->environ.glExtensions, "GL_EXT_blend_color"))
  117.             glBlendColorEXT(.5, .5, .5, .5);
  118. #endif
  119. #if defined(GL_EXT_blend_logic_op) || defined(GL_EXT_blend_minmax) || defined(GL_EXT_blend_subtract)
  120.     if (
  121.  #ifdef GL_EXT_blend_logic_op
  122.         (this->blendEq == GL_LOGIC_OP &&
  123.         strstr(this->environ.glExtensions, "GL_EXT_blend_logic_op")) ||
  124.  #endif
  125.  #ifdef GL_EXT_blend_minmax
  126.         ((this->blendEq == GL_MIN_EXT || this->blendEq == GL_MAX_EXT) &&
  127.         strstr(this->environ.glExtensions, "GL_EXT_blend_minmax")) ||
  128.  #endif
  129.  #ifdef GL_EXT_blend_subtract
  130.         ((this->blendEq == GL_FUNC_SUBTRACT_EXT || this->blendEq == GL_FUNC_REVERSE_SUBTRACT_EXT) &&
  131.         strstr(this->environ.glExtensions, "GL_EXT_blend_subtract")) ||
  132.  #endif
  133.         (this->blendEq == GL_FUNC_ADD_EXT))
  134.         glBlendEquationEXT(this->blendEq);
  135.     else
  136.         return -1;
  137. #endif
  138. #ifdef GL_EXT_blend_logic_op
  139.         if (this->blendEq == GL_LOGIC_OP)
  140.             glLogicOp(this->logicOp ? this->logicOp : GL_COPY);
  141. #endif
  142. #ifdef GL_EXT_blend_color
  143.     if ((this->srcBlend == GL_CONSTANT_COLOR_EXT ||
  144.         this->srcBlend == GL_ONE_MINUS_CONSTANT_COLOR_EXT ||
  145.         this->srcBlend == GL_CONSTANT_ALPHA_EXT ||
  146.         this->srcBlend == GL_ONE_MINUS_CONSTANT_ALPHA_EXT ||
  147.         this->dstBlend == GL_CONSTANT_COLOR_EXT ||
  148.         this->dstBlend == GL_ONE_MINUS_CONSTANT_COLOR_EXT ||
  149.         this->dstBlend == GL_CONSTANT_ALPHA_EXT ||
  150.         this->dstBlend == GL_ONE_MINUS_CONSTANT_ALPHA_EXT) &&
  151.         !strstr(this->environ.glExtensions, "GL_EXT_blend_color")) return -1;
  152. #endif
  153.         glBlendFunc(this->srcBlend, this->dstBlend);
  154.         glEnable(GL_BLEND);
  155.     }
  156.  
  157.     if (!this->logicOp) {
  158.         glDisable(GL_LOGIC_OP);
  159.     } else {
  160.         glLogicOp(this->logicOp);
  161.         glEnable(GL_LOGIC_OP);
  162.     }
  163.  
  164. #ifdef GL_SGIS_MULTISAMPLE
  165.     if (!this->multisample) {
  166.     glDisable(GL_MULTISAMPLE_SGIS);
  167.     } else {
  168.     glEnable(GL_MULTISAMPLE_SGIS);
  169.     }
  170. #endif
  171.  
  172.     if (!this->texture) {
  173.         glDisable(GL_TEXTURE_1D);
  174.         glDisable(GL_TEXTURE_2D);
  175. #ifdef GL_EXT_texture3D
  176.     if (strstr(this->environ.glExtensions, "GL_EXT_texture3D"))
  177.             glDisable(GL_TEXTURE_3D_EXT);
  178. #endif
  179. #ifdef GL_SGIS_texture4D
  180.     if (strstr(this->environ.glExtensions, "GL_SGIS_texture4D"))
  181.             glDisable(GL_TEXTURE_4D_SGIS);
  182. #endif
  183.     if (this->textureData == PerVertex) this->textureData = None;
  184.     } else {
  185.         int mipmap;
  186.     int noborderW, noborderH, noborderD, noborderV;
  187.  
  188.     /* Get out of here if we're gonna use unsupported extensions! */
  189. #ifdef GL_SGIS_sharpen_texture
  190.         if ((this->texMagFilter == GL_LINEAR_SHARPEN_SGIS ||
  191.             this->texMagFilter == GL_LINEAR_SHARPEN_ALPHA_SGIS ||
  192.             this->texMagFilter == GL_LINEAR_SHARPEN_COLOR_SGIS) &&
  193.         !strstr(this->environ.glExtensions, "GL_SGIS_sharpen_texture"))
  194.         return -1;
  195. #endif
  196. #ifdef GL_SGIS_detail_texture
  197.         if ((this->texMagFilter == GL_LINEAR_DETAIL_SGIS ||
  198.             this->texMagFilter == GL_LINEAR_DETAIL_ALPHA_SGIS ||
  199.             this->texMagFilter == GL_LINEAR_DETAIL_COLOR_SGIS) &&
  200.         !strstr(this->environ.glExtensions, "GL_SGIS_detail_texture"))
  201.         return -1;
  202. #endif
  203. #ifdef GL_SGIS_texture_filter4
  204.         if ((this->texMagFilter == GL_FILTER4_SGIS ||
  205.             this->texMinFilter == GL_FILTER4_SGIS) &&
  206.         !strstr(this->environ.glExtensions, "GL_SGIS_texture_filter4"))
  207.         return -1;
  208. #endif
  209. #ifdef GL_SGIS_texture_border_clamp
  210.     if ((this->texWrapS == GL_CLAMP_TO_BORDER_SGIS ||
  211.         this->texWrapT == GL_CLAMP_TO_BORDER_SGIS ||
  212.  #ifdef GL_EXT_texture3D
  213.         this->texWrapR == GL_CLAMP_TO_BORDER_SGIS ||
  214.  #endif
  215.  #ifdef GL_SGIS_texture4D
  216.         this->texWrapQ == GL_CLAMP_TO_BORDER_SGIS ||
  217.  #endif
  218.         0) &&
  219.         !strstr(this->environ.glExtensions, "GL_SGIS_texture_border_clamp"))
  220.         return -1;
  221. #endif
  222. #ifdef GL_SGIS_texture_edge_clamp
  223.     if ((this->texWrapS == GL_CLAMP_TO_EDGE_SGIS ||
  224.         this->texWrapT == GL_CLAMP_TO_EDGE_SGIS ||
  225.  #ifdef GL_EXT_texture3D
  226.         this->texWrapR == GL_CLAMP_TO_EDGE_SGIS ||
  227.  #endif
  228.  #ifdef GL_SGIS_texture4D
  229.         this->texWrapQ == GL_CLAMP_TO_EDGE_SGIS ||
  230.  #endif
  231.         0) &&
  232.         !strstr(this->environ.glExtensions, "GL_SGIS_texture_edge_clamp"))
  233.         return -1;
  234. #endif
  235. #ifdef GL_EXT_texture3D
  236.     if (this->texture == GL_TEXTURE_3D_EXT &&
  237.         !strstr(this->environ.glExtensions, "GL_EXT_texture3D"))
  238.         return -1;
  239. #endif
  240. #ifdef GL_SGIS_texture4D
  241.     if (this->texture == GL_TEXTURE_4D_SGIS &&
  242.         !strstr(this->environ.glExtensions, "GL_SGIS_texture4D"))
  243.         return -1;
  244. #endif
  245.  
  246.         /* Figure if we need to create mipmaps or not */
  247. #ifdef GL_SGIS_filter4
  248.     if (this->texMinFilter == GL_NEAREST || 
  249.         this->texMinFilter == GL_LINEAR || 
  250.         this->texMinFilter == GL_FILTER4_SGIS)
  251. #else
  252.         if (this->texMinFilter == GL_NEAREST || 
  253.         this->texMinFilter == GL_LINEAR)
  254. #endif
  255. #ifdef GL_SGIS_sharpen_texture
  256.         if (this->texMagFilter == GL_LINEAR_SHARPEN_SGIS ||
  257.         this->texMagFilter == GL_LINEAR_SHARPEN_ALPHA_SGIS ||
  258.         this->texMagFilter == GL_LINEAR_SHARPEN_COLOR_SGIS)
  259.         mipmap = 1;
  260.         else
  261. #endif
  262.         mipmap = 0;
  263.     else
  264.         mipmap = 1;
  265.  
  266.         /* Fill in dimensions that aren't defined (for reporting and possible use later) */
  267.         switch (this->texture) {
  268.         case GL_TEXTURE_1D:
  269.             this->texHeight = 1;
  270.         case GL_TEXTURE_2D:
  271.             this->texDepth = 1;
  272. #ifdef GL_EXT_texture3D
  273.         case GL_TEXTURE_3D_EXT:
  274. #endif
  275.             this->texExtent = 1;
  276. #ifdef GL_SGIS_texture4D
  277.     case GL_TEXTURE_4D_SGIS:
  278. #endif
  279.         default:
  280.             break;
  281.         }
  282.  
  283.         /* Figure dimensionality of our texture image */
  284.         switch (this->texture) {
  285.         case GL_TEXTURE_1D:
  286.             this->texDim = 1;
  287.             break;
  288.         case GL_TEXTURE_2D:
  289.             this->texDim = 2;
  290.             break;
  291. #ifdef GL_EXT_texture3D
  292.         case GL_TEXTURE_3D_EXT:
  293.             this->texDim = 3;
  294.             break;
  295. #endif
  296. #ifdef GL_SGIS_texture4D
  297.         case GL_TEXTURE_4D_SGIS:
  298.             this->texDim = 4;
  299.             break;
  300. #endif
  301.         default:
  302.             break;
  303.         }
  304.  
  305.         /* Make sure everything's a power of two with the border removed */
  306.         noborderW = this->texWidth - this->texBorder * 2;
  307.         if (noborderW & noborderW - 1) return -1;
  308.         if (this->texDim > 1) {
  309.             int noborderH = this->texHeight - this->texBorder * 2;
  310.             if (noborderH & noborderH - 1) return -1;
  311.         }
  312.         if (this->texDim > 2) {
  313.             int noborderD = this->texDepth - this->texBorder * 2;
  314.             if (noborderD & noborderD - 1) return -1;
  315.         }
  316.         if (this->texDim > 3) {
  317.             int noborderV = this->texExtent - this->texBorder * 2;
  318.             if (noborderV & noborderV - 1) return -1;
  319.         }
  320.  
  321.         /* Set texture parameters */
  322.         glTexParameteri(this->texture, GL_TEXTURE_MAG_FILTER, this->texMagFilter);
  323.         glTexParameteri(this->texture, GL_TEXTURE_MIN_FILTER, this->texMinFilter);
  324.         glTexParameteri(this->texture, GL_TEXTURE_WRAP_S, this->texWrapS);
  325.         glTexParameteri(this->texture, GL_TEXTURE_WRAP_T, this->texWrapT);
  326. #ifdef GL_EXT_texture3D
  327.     if (strstr(this->environ.glExtensions, "GL_EXT_texture3D"))
  328.             glTexParameteri(this->texture, GL_TEXTURE_WRAP_R_EXT, this->texWrapR);
  329. #endif
  330. #ifdef GL_SGIS_texture4D
  331.     if (strstr(this->environ.glExtensions, "GL_SGIS_texture4D"))
  332.             glTexParameteri(this->texture, GL_TEXTURE_WRAP_Q_SGIS, this->texWrapQ);
  333. #endif
  334.         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, this->texFunc);
  335.         if (this->texDim == 1)
  336.             glEnable(GL_TEXTURE_1D);
  337.         else
  338.             glDisable(GL_TEXTURE_1D);
  339.         if (this->texDim == 2)
  340.             glEnable(GL_TEXTURE_2D);
  341.         else
  342.             glDisable(GL_TEXTURE_2D);
  343. #ifdef GL_EXT_texture3D
  344.         if (this->texDim == 3)
  345.             glEnable(GL_TEXTURE_3D_EXT);
  346.         else
  347.             glDisable(GL_TEXTURE_3D_EXT);
  348. #endif
  349. #ifdef GL_SGIS_texture4d
  350.         if (this->texDim == 4)
  351.             glEnable(GL_TEXTURE_4D_SGIS);
  352.         else
  353.             glDisable(GL_TEXTURE_4D_SGIS);
  354. #endif
  355.  
  356. #ifdef GL_SGIX_texture_scale_bias
  357.     /* Define post texture filter scale and bias values */
  358.     glTexParameterfv(this->texture, 
  359.                          GL_POST_TEXTURE_FILTER_SCALE_SGIX, 
  360.                          &this->texRedScale);
  361.     glTexParameterfv(this->texture, 
  362.                          GL_POST_TEXTURE_FILTER_BIAS_SGIX, 
  363.                          &this->texRedBias);
  364. #endif
  365.  
  366. #ifdef GL_SGI_texture_color_table
  367.     /* Define texture color table if appropriate */
  368.     if (this->texColorTable) {
  369.         void *texColorTable;
  370.         GLint texColorTableSize;
  371.  
  372.         /* First see if color table extension is supported */
  373.             if (!strstr(this->environ.glExtensions, "GL_SGI_texture_color_table")) return -1;
  374.  
  375.             /* Then verify that its width is a power of 2 */
  376.             if (this->texColorTableWidth & this->texColorTableWidth - 1) return -1;
  377.  
  378.         /* Create and define texture color table */
  379.             texColorTable = new_ImageData(this->texColorTableWidth, 1,
  380.                                           GL_RGBA, GL_UNSIGNED_BYTE,
  381.                                           4, False, False, 0, &texColorTableSize);
  382.             glColorTableSGI(GL_TEXTURE_COLOR_TABLE_SGI, this->texColorTableInternalFormat, 
  383.                             this->texColorTableWidth,
  384.                             GL_RGBA, GL_UNSIGNED_BYTE, texColorTable);
  385.             AlignFree(texColorTable);
  386.  
  387.         glEnable(GL_TEXTURE_COLOR_TABLE_SGI);
  388.     } else {
  389.         glDisable(GL_TEXTURE_COLOR_TABLE_SGI);
  390.     }
  391. #endif
  392.  
  393.         /* Create images and define them */
  394.         if (mipmap) {
  395.             int imagewidth, imageheight, imagedepth, imageextent;
  396.             int width, height, depth, extent;
  397.             int realwidth, realheight, realdepth, realextent;
  398.             int n = 0;
  399.             void* mipmap;
  400.  
  401.             /* Strip borders off image dimensions so figuring the mipmaps is easier */
  402.             imagewidth = this->texWidth - 2 * this->texBorder;
  403.             if (this->texDim > 1)
  404.                 imageheight = this->texHeight - 2 * this->texBorder;
  405.         else
  406.         imageheight = 1;
  407.             if (this->texDim > 2)
  408.                 imagedepth = this->texDepth - 2 * this->texBorder;
  409.         else
  410.         imagedepth = 1;
  411.             if (this->texDim > 3)
  412.                 imageextent = this->texExtent - 2 * this->texBorder;
  413.         else
  414.         imageextent = 1;
  415.  
  416.             for (width = imagewidth, height = imageheight, depth = imagedepth, extent = imageextent;
  417.                 width >= 1 || height >= 1 || depth >= 1 || extent >= 1;
  418.                 width /= 2, height /= 2, depth /= 2, extent /= 2, n++) {
  419.                 realwidth = max(width, 1);
  420.                 realheight = max(height, 1);
  421.                 realdepth = max(depth, 1);
  422.                 realextent = max(extent, 1);
  423.                 realwidth += 2 * this->texBorder;
  424.                 if (this->texDim > 1) realheight += 2 * this->texBorder;
  425.                 if (this->texDim > 2) realdepth += 2 * this->texBorder;
  426.                 if (this->texDim > 3) realextent += 2 * this->texBorder;
  427.                 mipmap = MakeTexImage(realwidth,
  428.                     realheight,
  429.                     realdepth,
  430.                     realextent,
  431.                     GL_RGBA,
  432.                     GL_UNSIGNED_SHORT,
  433.                     4,
  434.                     GL_FALSE,
  435.                     GL_FALSE,
  436.                     this->memAlignment,
  437.                     &size);
  438.                 DefineTexImage(this->texture, n, this->texComps, 
  439.                     realwidth, realheight, realdepth, realextent,
  440.                     this->texBorder, GL_RGBA, GL_UNSIGNED_SHORT, mipmap);
  441.                 AlignFree(mipmap);
  442.             }
  443.         } else {
  444.             void* image = MakeTexImage(this->texWidth,
  445.                 this->texHeight,
  446.                 this->texDepth,
  447.                 this->texExtent,
  448.                 GL_RGBA,
  449.                 GL_UNSIGNED_SHORT,
  450.                 4,
  451.                 GL_FALSE,
  452.                 GL_FALSE,
  453.                 this->memAlignment,
  454.                 &size);
  455.             DefineTexImage(this->texture, 0, this->texComps, 
  456.                 this->texWidth, this->texHeight, this->texDepth, this->texExtent,
  457.                 this->texBorder, GL_RGBA, GL_UNSIGNED_SHORT, image);
  458.             AlignFree(image);
  459.  
  460.         }
  461.  
  462. #ifdef GL_SGIS_detail_texture
  463.         /* Create detail texture image, if appropriate */
  464.         if (this->texMagFilter == GL_LINEAR_DETAIL_SGIS ||
  465.             this->texMagFilter == GL_LINEAR_DETAIL_ALPHA_SGIS ||
  466.             this->texMagFilter == GL_LINEAR_DETAIL_COLOR_SGIS) {
  467.             void *image;
  468.             if (this->detailWidth & this->detailWidth - 1 ||
  469.                 this->detailHeight & this->detailHeight - 1 ||
  470.         !strstr(this->environ.glExtensions, "GL_SGIS_detail_texture"))
  471.         return -1;
  472.             image = MakeTexImage(this->detailWidth,
  473.                 this->detailHeight,
  474.                 1,
  475.                 1,
  476.                 GL_RGBA,
  477.                 GL_UNSIGNED_SHORT,
  478.                 4,
  479.                 GL_FALSE,
  480.                 GL_FALSE,
  481.                 this->memAlignment,
  482.                 &size);
  483.             glTexParameteri(GL_DETAIL_TEXTURE_2D_SGIS, GL_DETAIL_TEXTURE_LEVEL_SGIS, this->detailLevel);
  484.             glTexParameteri(GL_DETAIL_TEXTURE_2D_SGIS, GL_DETAIL_TEXTURE_MODE_SGIS, this->detailMode);
  485.             DefineTexImage(GL_DETAIL_TEXTURE_2D_SGIS, 0, this->texComps,
  486.                 this->detailWidth, this->detailHeight, 1, 1,
  487.                 0, GL_RGBA, GL_UNSIGNED_SHORT, image);
  488.             AlignFree(image);
  489.         }
  490. #endif
  491.  
  492. #ifdef GL_SGIS_texture_select
  493.     if (GL_DUAL_ALPHA4_SGIS <= this->texComps && 
  494.             this->texComps <= GL_DUAL_LUMINANCE_ALPHA8_SGIS) {
  495.         glTexParameteri(this->texture, GL_DUAL_TEXTURE_SELECT_SGIS, this->texSelect);
  496.     } else if (GL_QUAD_ALPHA4_SGIS <= this->texComps &&
  497.                    this->texComps <= GL_QUAD_INTENSITY8_SGIS) {
  498.         glTexParameteri(this->texture, GL_QUAD_TEXTURE_SELECT_SGIS, this->texSelect);
  499.     }
  500. #endif
  501.     }
  502.  
  503.     if (!this->texGen) {
  504.         glDisable(GL_TEXTURE_GEN_S);
  505.         glDisable(GL_TEXTURE_GEN_T);
  506.     } else {
  507.         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, this->texGen);
  508.         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, this->texGen);
  509.         glEnable(GL_TEXTURE_GEN_S);
  510.         glEnable(GL_TEXTURE_GEN_T);
  511.     }
  512.  
  513.     return 0;
  514. }
  515.  
  516. void CalcRGBColor(GLfloat u, GLfloat* r, GLfloat* g, GLfloat* b)
  517. {
  518.     /* These should look familiar... (Second degree Bernstein basis) */
  519.     *r = (1.0 - u) * (1.0 - u);
  520.     *g = 2.0 * u * (1.0 - u);
  521.     *b = u * u;
  522. }
  523.  
  524. static GLfloat Clamp0to1(GLfloat x)
  525. {
  526.     return (fabs(x) - fabs(x-1.)) * .5 + .5;
  527. }
  528.  
  529. void AddColorRGBData(GLfloat* data, GLfloat x, GLfloat y, GLfloat cf)
  530. {
  531.     GLfloat rx, gx, bx, ry, gy, by;
  532.  
  533.     CalcRGBColor((x+1.0)/2.0, &rx, &gx, &bx);
  534.     CalcRGBColor((y+1.0)/2.0, &gy, &ry, &by);
  535.     *data++ = Clamp0to1(cf * (rx + ry) * 3.0 / 2.0);
  536.     *data++ = Clamp0to1(cf * (gx + gy) * 3.0 / 2.0);
  537.     *data++ = Clamp0to1(cf * (bx + by) * 3.0 / 2.0);
  538. }
  539.  
  540. void AddColorRGBAData(GLfloat* data, GLfloat x, GLfloat y, GLfloat cf)
  541. {
  542.     GLfloat rx, gx, bx, ax, ry, gy, by, ay;
  543.  
  544.     CalcRGBColor((x+1.0)/2.0, &rx, &gx, &bx);
  545.     CalcRGBColor((y+1.0)/2.0, &gy, &ry, &by);
  546.     *data++ = Clamp0to1(cf * (rx + ry) * 3.0 / 2.0);
  547.     *data++ = Clamp0to1(cf * (gx + gy) * 3.0 / 2.0);
  548.     *data++ = Clamp0to1(cf * (bx + by) * 3.0 / 2.0);
  549.     *data++ = Clamp0to1((x + y + 2.)/4.);
  550. }
  551.  
  552. void AddColorCIData(GLfloat* data, GLfloat x, GLfloat y, int dim, int rampsize)
  553. {
  554.     GLfloat index = (GLfloat)((int)((x + 1.0)/2.0 * dim) % 128 + 63)/255.*(GLfloat)rampsize;
  555.     *data++ = index;
  556. }
  557.  
  558. #ifdef GL_SGI_array_formats
  559. void AddColorCIDataUI(GLfloat* data, GLfloat x, GLfloat y, int dim, int rampsize)
  560. {
  561.     GLuint index = (GLuint)((int)((x + 1.0)/2.0 * dim) % 128 + 63)/255.*(GLfloat)rampsize;
  562.     GLuint *idata = (GLuint *) data;
  563.     *idata = index;
  564. }
  565. #endif
  566.  
  567. void AddTexture1DData(GLfloat* data, GLfloat x, GLfloat y, GLfloat tfx)
  568. {
  569.     *data++ = (tfx * x + 1.)/2.;
  570. }
  571.  
  572. void AddTexture2DData(GLfloat* data, GLfloat x, GLfloat y, GLfloat tfx, GLfloat tfy)
  573. {
  574.     *data++ = (tfx * x + 1.)/2.;
  575.     *data++ = (tfy * y + 1.)/2.;
  576. }
  577.  
  578. void AddTexture3DData(GLfloat* data, GLfloat x, GLfloat y, GLfloat tfx, GLfloat tfy, GLfloat tfz)
  579. {
  580.     *data++ = (tfx * x + 1.)/2.;
  581.     *data++ = (tfy * y + 1.)/2.;
  582.     *data++ = (tfz * y + 1.)/2.;
  583. }
  584.